Attribute VB_Name = "Lib"
Option Explicit

Public Type TSettings
 ConnString As String
End Type

Private Const RegPath As String = "HKLM\Software\MyGroupManager\"

Public AppSettings As TSettings
Public ErCode As Byte

Private Sub SaveReg(MyKey As String, MyValue As String)
 Dim objWSH
 On Error Resume Next
 Set objWSH = CreateObject("WScript.Shell")
 objWSH.RegWrite RegPath & MyKey, MyValue
 Set objWSH = Nothing
 On Error GoTo 0
End Sub 'SaveReg

Private Sub DelReg(MyKey As String)
 Dim objWSH
 On Error Resume Next
 Set objWSH = CreateObject("WScript.Shell")
 objWSH.RegDelete RegPath & MyKey
 Set objWSH = Nothing
 On Error GoTo 0
End Sub 'DelReg

Private Function ReadReg(MyKey)
 Dim objWSH
 Dim RKey As String
 Set objWSH = CreateObject("WScript.Shell")
 RKey = RegPath & MyKey
 On Error Resume Next
 ReadReg = objWSH.RegRead(RKey)
 On Error GoTo 0
 Set objWSH = Nothing
End Function 'ReadReg

Public Sub LoadAppSettings()
 With Lib.AppSettings
  .ConnString = ReadReg("ConnString")
 End With
End Sub

Public Sub SaveAppSettings()
 With Lib.AppSettings
  SaveReg "ConnString", .ConnString
 End With
End Sub

Public Sub TerminateApplication()
 Lib.AdLog "Closing database"
 ErCode = Database.CloseDb
 Lib.AdLog "Terminating MyGroupManager"
 End
End Sub

Public Function GroupName(Description As String) As String
 Dim L As Integer
 L = Len(Description)
 GroupName = Right(Description, L - 6)
End Function

Public Function Pure(S As String) As String
 Pure = Replace(S, "'", "''")
End Function

Private Function LogFileName() As String
 LogFileName = App.Path & "\MyGroupManagerLogs.txt"
End Function

Public Sub SayError(S As String)
 MsgBox S, vbCritical, "MyGroupManager error"
 Lib.AdLog "[ERROR] : " & S
End Sub

Public Sub AdLog(S As String)
 Dim MsgLog As String
 Dim Tmp As String
 Dim Fid As Long
 Dim Cnt As Boolean
 On Error Resume Next
    'vbCrlf
    MsgLog = S
    Tmp = Now & " - " & MsgLog
    Fid = FreeFile
    Open LogFileName For Append As #Fid
     Write #Fid, Tmp
    Close #Fid
    MsgLog = ""
 DoEvents
End Sub

Public Sub ClearLogFiles()
 Lib.DeleteFile LogFileName
 Lib.AdLog "Flushing the logs..."
End Sub

Private Sub DeleteFile(FileName As String)
 Dim FSO As New Scripting.FileSystemObject
 On Error Resume Next
 FSO.DeleteFile (FileName)
 Set FSO = Nothing
End Sub

Private Sub Main()
 XPStyle.InitXPStyle
 Lib.AdLog "Starting MyGroupManager"
 Lib.LoadAppSettings
 Lib.AdLog "Settings loaded"
 ErCode = Database.OpenDb
 FrmMain.Show
End Sub
